/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import DAO.*;
import POJO.*;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author ben
 */
public class form_GheNgoi extends javax.swing.JInternalFrame {

    /**
     * Creates new form form_GheNgoi
     * @throws java.sql.SQLException
     */
    public form_GheNgoi() throws SQLException {
        initComponents();
        
        Load();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    DefaultComboBoxModel cmodel;
    DAO_GheNgoi gn = new DAO_GheNgoi();
    DefaultTableModel model = new DefaultTableModel();
    
    public void Load() throws SQLException{
        List<POJO_GheNgoi> list = gn.LoadList();
        model = new DefaultTableModel(null, new Object[]{
            "Mã Ghế", "Mã đoàn tàu", "Loại ghế", "Tên toa", "Mã phòng", "Tầng"});
        for(POJO_GheNgoi ghengoi : list){
            model.addRow(new Object[]{
                ghengoi.getMaGhe(),
                ghengoi.getMaDT(),
                ghengoi.getLoaiGhe(),
                ghengoi.getTenToa(),
                ghengoi.getMaPhong(),
                ghengoi.getTang()});
        };
    
        tbGheNgoi.setModel(model);
        
        DAO_DoanTau dt = new DAO_DoanTau();
        List<POJO_DoanTau> doanTaus = dt.LoadList();
        cmodel = new DefaultComboBoxModel(doanTaus.toArray());
        cmbMaDT.setModel(cmodel);
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtTenToa = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtMaGhe = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbGheNgoi = new javax.swing.JTable();
        cmbMaDT = new javax.swing.JComboBox();
        txtTang = new javax.swing.JTextField();
        btnXoa = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btnSua = new javax.swing.JButton();
        txtLoaiGhe = new javax.swing.JTextField();
        btnThem = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnThoat = new javax.swing.JButton();
        txtMaPhong = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        jLabel5.setText("Tên toa:");

        jLabel2.setText("Mã ghế:");

        tbGheNgoi.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Mã ghế", "Mã đoàn tàu", "Loại ghế", "Tên toa", "Mã phòng", "Tầng"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbGheNgoi.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbGheNgoiMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbGheNgoi);

        cmbMaDT.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        jLabel1.setText("Mã đoàn tàu:");

        jLabel6.setText("Tầng:");

        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        jLabel9.setText("Loại ghế:");

        jLabel4.setText("Mã phòng:");

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        jLabel3.setText("QUẢN LÝ GHẾ NGỒI");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(43, 43, 43))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(txtLoaiGhe, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel2))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtMaGhe, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(cmbMaDT, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 154, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtMaPhong, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtTenToa)
                                    .addComponent(txtTang, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(291, 291, 291)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(txtMaGhe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtTenToa, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 15, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMaPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel1)
                    .addComponent(cmbMaDT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtLoaiGhe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(txtTang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(57, 57, 57)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnXoa)
                    .addComponent(btnThem)
                    .addComponent(btnSua)
                    .addComponent(btnThoat))
                .addGap(49, 49, 49))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbGheNgoiMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbGheNgoiMouseClicked
        // TODO add your handling code here:
         int index = tbGheNgoi.getSelectedRow();
        
        txtMaGhe.setText(String.valueOf(model.getValueAt(index, 0)));
        cmodel.setSelectedItem(String.valueOf(model.getValueAt(index, 1)));
        txtLoaiGhe.setText(String.valueOf(model.getValueAt(index, 2)));
        txtTenToa.setText(String.valueOf(model.getValueAt(index, 3)));
        txtMaPhong.setText(String.valueOf(model.getValueAt(index, 4)));
        txtTang.setText(String.valueOf(model.getValueAt(index, 5)));
    }//GEN-LAST:event_tbGheNgoiMouseClicked

     public boolean TestEmpty(){
        return !txtMaGhe.getText().isEmpty() && !txtLoaiGhe.getText().isEmpty() && !txtTenToa.getText().isEmpty() && !txtMaPhong.getText().isEmpty() && !txtTang.getText().isEmpty();
    }
    
    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        // TODO add your handling code here:
        if(TestEmpty() == true){
            POJO_GheNgoi ghengoi = new POJO_GheNgoi();
            
            ghengoi.setMaGhe(txtMaGhe.getText());
            ghengoi.setMaDT(cmodel.getSelectedItem().toString());
            ghengoi.setLoaiGhe(txtLoaiGhe.getText());
            ghengoi.setTenToa(txtTenToa.getText());
            ghengoi.setMaPhong(txtMaPhong.getText());
            ghengoi.setTang(Integer.valueOf(txtTang.getText()));
            
            gn.themGheNgoi(ghengoi);
            
            try {
                Load();
            } catch (SQLException ex) {
                Logger.getLogger(form_GheNgoi.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            JOptionPane.showMessageDialog(null, "Thêm thành công");
        }
        else{
            JOptionPane.showMessageDialog(null, "Xin vui lòng nhập đầy đủ thông tin");
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
        int index = tbGheNgoi.getSelectedRow();
        
        if(index > 0){
            POJO_GheNgoi ghengoi = new POJO_GheNgoi();
            ghengoi.setMaGhe(txtMaGhe.getText());
            
            gn.xoaGheNgoi(ghengoi);
            
            try {
                Load();
            } catch (SQLException ex) {
                Logger.getLogger(form_GheNgoi.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            JOptionPane.showMessageDialog(null, "Xóa thành công");
        }
        else{
            JOptionPane.showMessageDialog(null, "Xin vui lòng nhập đầy đủ thông tin");
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        // TODO add your handling code here:
        if(TestEmpty() == true){
            POJO_GheNgoi ghengoi = new POJO_GheNgoi();
            ghengoi.setMaGhe(txtMaGhe.getText());
            
            ghengoi.setMaDT(cmodel.getSelectedItem().toString());
            
            ghengoi.setLoaiGhe(txtLoaiGhe.getText());
            ghengoi.setTenToa(txtTenToa.getText());
            ghengoi.setMaPhong(txtMaPhong.getText());
            ghengoi.setTang(Integer.parseInt(txtTang.getText()));
            
            gn.capNhatGheNgoi(ghengoi);
            
            try {
                Load();
            } catch (SQLException ex) {
                Logger.getLogger(form_GheNgoi.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            JOptionPane.showMessageDialog(null, "Thêm thành công");
        }
        else{
            JOptionPane.showMessageDialog(null, "Xin vui lòng nhập đầy đủ thông tin");
        }
    }//GEN-LAST:event_btnSuaActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cmbMaDT;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbGheNgoi;
    private javax.swing.JTextField txtLoaiGhe;
    private javax.swing.JTextField txtMaGhe;
    private javax.swing.JTextField txtMaPhong;
    private javax.swing.JTextField txtTang;
    private javax.swing.JTextField txtTenToa;
    // End of variables declaration//GEN-END:variables
}
